package reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

//获得类的信息
public class Test05 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
        Class c1 = Class.forName("reflection.User");
        //获得类的名字
        System.out.println(c1.getName());  //全限定类名
        System.out.println(c1.getSimpleName()); //类名

        //获得类的属性
        Field[] fields = c1.getFields();  //只能找到public属性
        fields = c1.getDeclaredFields();  //能找到全部的属性
        for (Field f : fields) {
            System.out.println(f);
        }
        System.out.println("======================");
        //获得指定属性的值
        Field name = c1.getDeclaredField("name");
        System.out.println(name);
        System.out.println("-------------------------");
        //获得方法的属性
        //获得自己的包括父类的所有的public方法
        Method[] methods = c1.getMethods();
        for (Method m : methods) {
            System.out.println(m);
        }
        System.out.println("-------------------------");
        //只获取自己的方法public和private都会获取到
        Method[] declaredMethods = c1.getDeclaredMethods();
        for (Method m : declaredMethods) {
            System.out.println(m);
        }
        System.out.println("=======================");
        //获得指定方法
        Method getName = c1.getMethod("getName", null);
        Method setName = c1.getMethod("setName", String.class);
        System.out.println(getName);
        System.out.println(setName);

        System.out.println("-------------------------");
        //获得构造器
        //获取公开构造器
        Constructor[] constructors = c1.getConstructors();
        for (Constructor co : constructors) {
            System.out.println(co);
        }
        //获取自己的所有构造方法
        Constructor[] declaredConstructors = c1.getDeclaredConstructors();
        for (Constructor co : declaredConstructors) {
            System.out.println(co);
        }
        System.out.println("-=-=-=-=-=-=-=-=-=");
        //获得指定的构造器
        Constructor constructor1 = c1.getDeclaredConstructor(String.class, int.class, int.class);
        System.out.println(constructor1);

    }
}
